
package com.android.sharecar.activity;

import com.android.sharecar.activity.config.AboutActivity;
import com.android.sharecar.activity.config.ConfigureActivity;
import com.android.sharecar.service.ShareCarBGService;
import com.android.sharecar.util.SysUtil;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TabHost;

/**
 * The main UI for user's operating
 * 
 * @author xinyan
 * @date 2011-9-13
 */
public class MainTabWidgetActivity extends TabActivity {
    public static final String TAG = "MainTabWidgetActivity";

    // public static MainTabWidgetActivity mInstance;

    // Bundle key to store value of which tab to setCurrent
    public static final String CURRENT_TAB = "tab";

    public static final int CURRENT_TAB_NORMAL = 0;

    public static final int CURRENT_TAB_PICKRIDE = 1;

    public static final int CURRENT_TAB_DRIVING = 2;

    public static final int CURRENT_TAB_RESERVATION = 3;

    public static final int CURRENT_TAB_TALK = 4;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tabhost_main);

        // MainTabWidgetActivity.mInstance = this;

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Reusable TabSpec for each tab
        Intent tabIntent; // Reusable Intent for each tab
        Intent dataIntent; // Data intent for storing data to tabIntent or this
                           // activity
        Bundle bundle;

        dataIntent = getIntent();
        bundle = dataIntent.getExtras();

        // Create an Intent to launch an Activity for the tab (to be reused)
        tabIntent = new Intent().setClass(this, StandardActivity.class);
        if (null != bundle && bundle.containsKey(StandardActivity.STATE)) {
            tabIntent.putExtras(bundle);
        }

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("standardTab").setIndicator("普通").setContent(tabIntent);
        tabHost.addTab(spec);

        // Create an Intent to launch an Activity for the tab (to be reused)
        tabIntent = new Intent().setClass(this, PickRideActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("pickrideTab").setIndicator("搭车").setContent(tabIntent);
        tabHost.addTab(spec);

        // Create an Intent to launch an Activity for the tab (to be reused)
        tabIntent = new Intent().setClass(this, ProvideRideAcitvity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("drivingTab").setIndicator("开车").setContent(tabIntent);
        tabHost.addTab(spec);

        // Create an Intent to launch an Activity for the tab (to be reused)
        tabIntent = new Intent().setClass(this, ReservationActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("reserveTab").setIndicator("预约").setContent(tabIntent);
        tabHost.addTab(spec);

        // Create an Intent to launch an Activity for the tab (to be reused)
        tabIntent = new Intent().setClass(this, TalkListActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("talkTab").setIndicator("发言").setContent(tabIntent);
        tabHost.addTab(spec);

        try {
            if (null != bundle && bundle.containsKey(CURRENT_TAB)) {
                tabHost.setCurrentTab(bundle.getInt(CURRENT_TAB));
            } else {
                tabHost.setCurrentTab(CURRENT_TAB_NORMAL);
            }
        } catch (Exception e) {
            tabHost.setCurrentTab(CURRENT_TAB_NORMAL);

            Log.e(TAG, "Bundle doesn't contain key CURRENT_TAB");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_interface_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        switch (itemId) {
            case R.id.menu_item_about:
                startActivity(new Intent(this, AboutActivity.class));

                break;

            case R.id.menu_item_exist:
                SysUtil util = new SysUtil(this);
                util.exit(); // exist the entire app directly.

                // Here we stop the bg service.
                this.stopService(new Intent(this, ShareCarBGService.class));

                break;

            case R.id.menu_item_configure:
                startActivity(new Intent(this, ConfigureActivity.class));

                break;

            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
